package com.ruoyi.business.service.impl;

import com.ruoyi.business.mapper.WsdCanteensMapper;
import com.ruoyi.business.service.IWsdCanteensService;
import com.ruoyi.common.core.domain.dto.WsdCanteensDTO;
import com.ruoyi.common.core.domain.entity.WsdCanteens;
import com.ruoyi.common.core.domain.entity.WsdCompany;
import com.ruoyi.common.core.domain.vo.CompanyVO;
import com.ruoyi.common.core.domain.vo.WsdCanteensVO;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 饭堂信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-15
 */
@Service
public class WsdCanteensServiceImpl implements IWsdCanteensService
{
    @Autowired
    private WsdCanteensMapper wsdCanteensMapper;

    /**
     * 查询饭堂信息
     *
     * @param id 饭堂信息主键
     * @return 饭堂信息
     */
    @Override
    public WsdCanteens selectWsdCanteensById(Long id)
    {
        return wsdCanteensMapper.selectWsdCanteensById(id);
    }

    /**
     * 查询饭堂信息列表
     *
     * @param wsdCanteens 饭堂信息
     * @return 饭堂信息
     */
    @Override
    public List<WsdCanteens> selectWsdCanteensList(WsdCanteens wsdCanteens) {
        return wsdCanteensMapper.selectWsdCanteensList(wsdCanteens);
    }

    /**
     * 新增饭堂信息
     *
     * @param wsdCanteensDTO 饭堂信息
     * @return 结果
     */
    @Override
    public int insertWsdCanteens(WsdCanteensDTO wsdCanteensDTO) {
        //String imagesAsString = String.join("||", wsdCanteensDTO.getImages());
        String imagesAsString = wsdCanteensDTO.getImages() != null ? String.join("||", wsdCanteensDTO.getImages()) : "";
        WsdCanteens wsdCanteens = new WsdCanteens();
        BeanUtils.copyProperties(wsdCanteensDTO,wsdCanteens);
        wsdCanteens.setImages(imagesAsString);
        wsdCanteens.setCreateTime(DateUtils.getNowDate());
        return wsdCanteensMapper.insertWsdCanteens(wsdCanteens);
    }

    /**
     * 修改饭堂信息
     *
     * @param wsdCanteensDTO 饭堂信息
     * @return 结果
     */
    @Override
    public int updateWsdCanteens(WsdCanteensDTO wsdCanteensDTO) {
//        String imagesAsString = String.join("||", wsdCanteensDTO.getImages());
        // 检查wsdCanteensDTO.getImages()是否为null
        String imagesAsString = wsdCanteensDTO.getImages() != null ? String.join("||", wsdCanteensDTO.getImages()) : "";
        WsdCanteens wsdCanteens = new WsdCanteens();
        BeanUtils.copyProperties(wsdCanteensDTO,wsdCanteens);
        wsdCanteens.setId(wsdCanteensDTO.getId());
        wsdCanteens.setImages(imagesAsString);
        wsdCanteens.setUpdateTime(DateUtils.getNowDate());
        return wsdCanteensMapper.updateWsdCanteens(wsdCanteens);
    }

    /**
     * 批量删除饭堂信息
     *
     * @param ids 需要删除的饭堂信息主键
     * @return 结果
     */
    @Override
    public int deleteWsdCanteensByIds(Long[] ids) {
        return wsdCanteensMapper.deleteWsdCanteensByIds(ids);
    }

    /**
     * 删除饭堂信息信息
     *
     * @param id 饭堂信息主键
     * @return 结果
     */
    @Override
    public int deleteWsdCanteensById(Long id) {
        return wsdCanteensMapper.deleteWsdCanteensById(id);
    }

    /**
     * 获取饭堂图片
     *
     * @param schoolId
     * @return
     */
    @Override
    public WsdCanteensVO selectWsdCanteensBySchoolId(Long schoolId) {
        WsdCanteens wsdCanteens = new WsdCanteens();
        wsdCanteens.setSchoolId(schoolId);
        WsdCanteensVO wsdCanteensVO = new WsdCanteensVO();
        List<WsdCanteens> wsdCanteensList = wsdCanteensMapper.selectWsdCanteensList(wsdCanteens);
        //拿出wsdCanteensList的第一条
        if (wsdCanteensList.size() > 0) {
            WsdCanteens canteens = wsdCanteensList.get(0);
            List<String> imagesList = new ArrayList<>();
            // 使用"||"作为分隔符分割图片信息字符串
            String[] images = canteens.getImages().split("\\|\\|");
            for (String image : images) {
                imagesList.add(image);
            }
            // 将从数据库查询的WsdCompany对象的属性复制到CompanyVO对象中
            BeanUtils.copyProperties(canteens, wsdCanteensVO);
            // 设置CompanyVO对象的图片列表和证书列表
            wsdCanteensVO.setImages(imagesList);
        }
        // 返回wsdCanteensVO对象
        return wsdCanteensVO;
    }
}
